package xml;

import jdk.nashorn.api.scripting.ScriptUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class ReadXML {
    public static final String XML_NAME = "books.xml";

    //读取，增加，修改，删除
    public static void main(String[] args) {
        //read();
        //searchByName("java高级特性");
        //searchByOrder("2");
        //addNewBook(4,"mysql高级",3,9,"mysql的用法和Sql的用法");

        //delByOrder("2");

        searchByName2("java");
    }

    //根据order删除book节点
    public static void delByOrder(String order) {
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read("books.xml");
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        //xpath的查询语法
        List<Element> list = doc.selectNodes("/books/book[@order='" + order + "']");
        if (list.size() == 0) {
            System.out.println("没有这门课");
            return;
        }
        Element e = list.get(0);
        e.getParent().remove(e);//关键
        //保存
        //加一个格式
        OutputFormat f = OutputFormat.createPrettyPrint();

        f.setEncoding("utf-8");
        try {
            XMLWriter xw = new XMLWriter(new FileOutputStream(XML_NAME), f);
            xw.write(doc);
            xw.close();
        } catch (IOException ee) {
            ee.printStackTrace();
        }
    }

    //增加
    public static void addNewBook(int order, String name, int diff, int time, String des) {

        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(XML_NAME);
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        Element root = doc.getRootElement();
        Element book = root.addElement("book");
        book.addAttribute("order", String.valueOf(order));
        Element ename = book.addElement("name");
        ename.setText(name);//作为节点的值
        Element etime = book.addElement("time");
        etime.setText(String.valueOf(time));
        Element difft = book.addElement("difficult");
        difft.setText(String.valueOf(diff));
        Element descp = book.addElement("description");
        descp.setText(des);
        //只改了内存中的doc对象，没有写到文件中去

        //加一个格式
        OutputFormat f = OutputFormat.createPrettyPrint();

        f.setEncoding("utf-8");
        try {
            XMLWriter xw = new XMLWriter(new FileOutputStream(XML_NAME), f);
            xw.write(doc);
            xw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //根据order查找
    public static void searchByOrder(String order) {
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read("books.xml");
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        //xpath的查询语法
        List<Element> list = doc.selectNodes("/books/book[@order='" + order + "']");
        if (list.size() == 0) {
            System.out.println("没有这门课");
            return;
        }
        Element e = list.get(0);
        order = e.attributeValue("order");
        //Element ename = (Element) e.selectNodes("name").get(0);
        //简单写法
        Element ename = (Element) e.selectSingleNode("name");
        String fname = ename.getText();//节点的值

        Element edif = (Element) e.selectSingleNode("difficult");
        int difficult = Integer.parseInt(edif.getText());

        Element etime = (Element) e.selectSingleNode("time");
        int time = Integer.parseInt(etime.getText());

        Element miaoshu = (Element) e.selectSingleNode("description");
        String des = miaoshu.getTextTrim();

        System.out.println("查找到的信息如下：");
        System.out.println("这门课程叫" + fname + ",学时" + time + ",难度是" + difficult + ",描述是：" + des + ",学习顺序是" + order);

    }

    //根据课程名找课程的全部信息
    public static void searchByName(String name) {
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read("books.xml");
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        //xpath的查询语法
        //doc.selectNodes("/shops/shop[itemno=?] or /shops/shop[name=?]");

        List<Element> list = doc.selectNodes("/books/book[name='" + name + "']");
        if (list.size() == 0) {
            System.out.println("没有这门课");
            return;
        }
        Element e = list.get(0);
        String order = e.attributeValue("order");
        //Element ename = (Element) e.selectNodes("name").get(0);
        //简单写法
        Element ename = (Element) e.selectSingleNode("name");
        String fname = ename.getText();//节点的值

        Element edif = (Element) e.selectSingleNode("difficult");
        int difficult = Integer.parseInt(edif.getText());

        Element etime = (Element) e.selectSingleNode("time");
        int time = Integer.parseInt(etime.getText());

        Element miaoshu = (Element) e.selectSingleNode("description");
        String des = miaoshu.getTextTrim();

        System.out.println("查找到的信息如下：");
        System.out.println("这门课程叫" + fname + ",学时" + time + ",难度是" + difficult + ",描述是：" + des + ",学习顺序是" + order);

    }

    public static void searchByName2(String name) {
        System.out.println("按照名称的模糊查找：");
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read("books.xml");
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        //xpath的查询语法
        //doc.selectNodes("/shops/shop[itemno=?] or /shops/shop[name=?]");

        List<Element> list = doc.selectNodes("/books/book[contains(name,'" + name + "')]");
        if (list.size() == 0) {
            System.out.println("没有这门课");
            return;
        }
        for (Element e : list) {

            String order = e.attributeValue("order");
            //Element ename = (Element) e.selectNodes("name").get(0);
            //简单写法
            Element ename = (Element) e.selectSingleNode("name");
            String fname = ename.getText();//节点的值

            Element edif = (Element) e.selectSingleNode("difficult");
            int difficult = Integer.parseInt(edif.getText());

            Element etime = (Element) e.selectSingleNode("time");
            int time = Integer.parseInt(etime.getText());

            Element miaoshu = (Element) e.selectSingleNode("description");
            String des = miaoshu.getTextTrim();

            System.out.println("查找到的信息如下：");
            System.out.println("这门课程叫" + fname + ",学时" + time + ",难度是" + difficult + ",描述是：" + des + ",学习顺序是" + order);
        }
    }

    //查询元素的方法，xpath...
    public static void read() {
        SAXReader reader = new SAXReader();
        try {
            Document doc = reader.read("books.xml");
            Element root = doc.getRootElement();
            System.out.println("根节点的名称是：" + root.getName());
            List<Element> list = root.selectNodes("book");
            for (Element e : list) {
                String order = e.attributeValue("order");
                //Element ename = (Element) e.selectNodes("name").get(0);
                //简单写法
                Element ename = (Element) e.selectSingleNode("name");
                String name = ename.getText();//节点的值

                Element edif = (Element) e.selectSingleNode("difficult");
                int difficult = Integer.parseInt(edif.getText());

                Element etime = (Element) e.selectSingleNode("time");
                int time = Integer.parseInt(etime.getText());

                Element miaoshu = (Element) e.selectSingleNode("description");
                String des = miaoshu.getTextTrim();

                System.out.println("这门课程叫" + name + ",学时" + time + ",难度是" + difficult + ",描述是：" + des + ",学习顺序是" + order);
            }
            //doc.selectNodes("/books/book");
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
    }
}
